<template>
  <div class="create-sfc-order">
    <el-dialog
      v-el-drag-dialog="{ drag: true, full: false, resize: false }"
      :visible="true"
      :close-on-click-modal="false"
      @close="closeMd"
      :append-to-body="true"
      width="350px"
      title="生成顺风车订单">
      <el-form
        :model="fm.ps"
        :ref="fm.fname"
        :label-width="'100px'"
        @keyup.enter.native="onSubmit">
        <el-form-item 
          label="支付类型"
          prop="payWay"
          :rules="[{required: true, message: '请选择支付类型'}]">
          <el-select
            v-model="fm.ps.payWay"
            placeholder="请选择支付类型"
            style="width: 100%">
            <el-option label="现金余额" value="CASHBALANCE"></el-option>
            <el-option label="垫支余额" value="JZBALANCE"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item 
          v-if="isSelfCreate == true"
          label="站 点"
          prop="stationId"
          :rules="[{required: true, message: '站点不能为空'}]">
          <el-select
            v-model="fm.ps.stationId"
            placeholder="请选择站点"
            style="width: 100%">
            <el-option 
              v-for="o in stationList"
              :key="o.id"
              :label="o.name" 
              :value="o.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            @click="onSubmit" 
            :loading="fm.sb.load">
            {{ fm.sb.text }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from "@/directive/el-drag-dialog";

import * as commonSer from '@/api/services/common.service';
import * as orderSer from '@/api/services/order.service';
export default {
  name: "CreateSfcOrder",
  directives: { elDragDialog },
  props: {myData: Object},
  data() {
    return {
      // 是否是手动生成订单
      isSelfCreate: this.myData.isSelfCreate || false,
      fm: {
        fname: "createSfcOrderFm",
        sb: { load: false, text: "立即生成" },
        ps: {
          // 生成的订单id数组字符串
          ids: this.myData.ids || '',
          // 支付方式
          payWay: null,
          // 站点ID（手动生成订单才有）
          stationId: null,
        },
      },

      // 站点列表
      stationList: [],
      
    };
  },
  created() {
    const that = this;
    console.log('this.myData', this.myData);
    that.getStationList();
  },
  methods: {
    /* 关闭弹框 */
    closeMd() {
      this.$emit("myClick", {state: false});
    },
    /**
     * 获取-接送机站点列表
     */
     getStationList(){
      const that = this;

      commonSer.getStationList({}).then(res => {
        if(res && res.code == 1){
          let list = res.data;

          list.forEach(it => {
            that.stationList.push({
              id: it.id,                    // id
              type: it.type,                // 类型 1-飞机 2-火车
              isSta: true,                  // 是否是站点地点
              lnglat: it.mapPoint.lngLat,   // 坐标
              area: it.county,              // 区域
              name: it.name,                // 地点简写
              address: it.mapPoint.address, // 地址
              note: it.nameNote             // 站点别名
            });
          });
        }
      });
    },
    /**
     * 提交表单
     */
    onSubmit() {
      const that = this, U = that.U, fm = that.fm, ps = U.scopy(fm.ps);

      that.$refs[fm.fname].validate((valid) => {
        if (!valid) {
          return false;
        } else {
          fm.sb.load = true;

          if(!ps.ids){
            return U.qerrorFalse('请先选择订单');
          }
          
          if(that.isSelfCreate){
            ps.id = ps.ids;
            delete ps.ids;

            orderSer.selefCreateSfcOrder(ps).then((res) => {
              fm.sb.load = false;

              if (!res || res.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                // 关闭弹框
                that.closeMd();

                that.$emit("reloadDg");
              }
            });
          }else{
            delete ps.stationId;

            orderSer.createSfcOrder(ps).then((res) => {
              fm.sb.load = false;

              if (!res || res.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                // 关闭弹框
                that.closeMd();

                that.$emit("reloadDg");
              }
            });
          }
        }
      });
    },
    
  },
};
</script>